package com.sanchunhui.psc.ui.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.kotlin.message.data.protocol.Message
import com.sanchunhui.psc.R
import com.sanchunhui.psc.ui.activity.TeamMembersActivity
import org.jetbrains.anko.find
import java.util.*

/**
 * @Author zyx
 * @Date 2021/5/19-16:16
 * @Dscribe
 */
class AlarmsMessageAdapter(val context: FragmentActivity?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var mDatas: List<Message> = ArrayList<Message>()

    fun setData(messages: List<Message>) {
        this.mDatas = messages
        notifyDataSetChanged()
    }

    companion object {
        val ITEM_TYPE_SEND_MESSAGE = 0
        val ITEM_TYPE_RECEIVE_MESSAGE = 1
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_message, parent, false)
        return  AlarmsMessageViewHolder(view)
    }

    override fun getItemCount(): Int = mDatas.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as AlarmsMessageViewHolder).bindData(mDatas[position])
    }

    inner class  AlarmsMessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val tv_role_name: TextView
        val tv_receive_message : TextView
        var tv_topic_timestamp : TextView

        lateinit var messages: Message

        init {

            tv_role_name = itemView.find(R.id.tv_role_name)
            tv_receive_message = itemView.find(R.id.tv_receive_message)
            tv_topic_timestamp = itemView.find(R.id.tv_topic_timestamp)

            itemView.setOnClickListener {
                val intent: Intent = Intent(context, TeamMembersActivity::class.java)
                context!!.startActivity(intent)
            }

        }

        fun bindData(messages: Message) {
            this.messages = messages
            tv_role_name.text = messages.msgTitle
            tv_receive_message.text = messages.msgContent
            tv_topic_timestamp.text = messages.msgTime

            //TODO:赋值其他字段
//            Picasso.with(context).load(mTeam.icon).into(iv_avatar)

        }
    }
}